package com.example.administrator.caigou51.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ab.util.AbSharedUtil;
import com.ab.util.AbViewUtil;
import com.alibaba.fastjson.JSON;
import com.example.administrator.caigou51.R;
import com.example.administrator.caigou51.activity.SearchActivity;
import com.example.administrator.caigou51.activity.SearchGoodsActivity;
import com.example.administrator.caigou51.bean.TitleBean;
import com.example.administrator.caigou51.global.App;
import com.example.administrator.caigou51.recyclerCard.basic.MaterialListView;
import com.example.administrator.caigou51.widget.labelListView.GameLabel;
import com.example.administrator.caigou51.widget.labelListView.IOnItemClickListener;
import com.example.administrator.caigou51.widget.labelListView.LabelListView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/8/19/019.
 */
@SuppressLint("ValidFragment")
public class SearchFragment  extends BaseFragment {
    private View holderView;
    private boolean isScaled = false;
    private boolean isHot = false;
    private MaterialListView material_listview;
    private LabelListView mLabelListView;
    private ArrayList<GameLabel> labelList = new ArrayList<GameLabel>();
    private List<TitleBean> titleBeanList;
    boolean isRepeat;
    public  SearchFragment(){};
    public  SearchFragment(List<TitleBean> titleBeanList,boolean tag)
    {
        this.titleBeanList=titleBeanList;
        isHot=tag;
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //  EventBus.getDefault().register(this);
        if (holderView == null) {
       /*     holderView = inflater.inflate(R.layout.template_materiallistview_layout, container, false);
            material_listview= (MaterialListView) holderView.findViewById(R.id.material_listview);
            material_listview.setColumnLayout(4,21);
            material_listview.setBackgroundColor(getResources().getColor(R.color.white));*/
            holderView = inflater.inflate(R.layout.morelabel, container, false);
            mLabelListView = (LabelListView) holderView.findViewById(R.id.label_list_view);
            mLabelListView.setSize(AbViewUtil.scaleTextValue(getActivity(), 26));
            initView();

        }
        ViewGroup parent = (ViewGroup) holderView.getParent();
        if (parent != null) {
            parent.removeView(holderView);
        }
        if (!isScaled) {
            AbViewUtil.scaleContentView((ViewGroup) holderView);
            isScaled = true;
        }
        return holderView;
    }

    private void initView() {
        for (int j = 0; j < titleBeanList.size(); j++) {
            //  SearchGoodsMainItemCard searchGoodsMainItemCard = new SearchGoodsMainItemCard(getActivity());
            // material_listview.add(searchGoodsMainItemCard);
            GameLabel label = new GameLabel();

            label.name =titleBeanList.get(j).getTitle();
            if ((j==0 && isHot) || (j==2 && isHot) || (j==6 && isHot)) {
                label.textColor = "1ca146";
            }else
            {
                label.textColor = "999999";
            }
            label.backgroudColor = "f5f5f5";
            label.strokeColor = "f5f5f5";
            labelList.add(label);

        }
        mLabelListView.setData(labelList);

        mLabelListView.setIOnItemClickListener(new IOnItemClickListener() {

            @Override
            public void onClick(String name, int position) {

            /*    Toast.makeText(getActivity(),
                        "标签内容：" + mLabelListView.getChildCount() + name + "   位置:" + position,
                        Toast.LENGTH_SHORT).show();*/
                String json=AbSharedUtil.getString(App.getInstance().getApplicationContext(),SearchActivity.SearchBeanList);
                //保存最近点击过的标签
                isRepeat=false;
                if (!TextUtils.isEmpty(json)) {
                    List<TitleBean> list = JSON.parseArray(json,TitleBean.class);

                    for (int i=0;i<list.size();i++)
                    {
                        if (list.get(i).getTitle().equals(titleBeanList.get(position).getTitle()))
                        {
                            isRepeat=true;
                            break;
                        }
                    }
                    if (!isRepeat) {
                        list.add(titleBeanList.get(position));
                        AbSharedUtil.putString(App.getInstance().getApplicationContext(), SearchActivity.SearchBeanList, JSON.toJSONString(list));
                    }
                }else
                {
                    List<TitleBean> list = new ArrayList<TitleBean>();
                    list.add(titleBeanList.get(position));
                    AbSharedUtil.putString(App.getInstance().getApplicationContext(), SearchActivity.SearchBeanList, JSON.toJSONString(list));
                }
                Intent intent=new Intent(getActivity(), SearchGoodsActivity.class);
                intent.putExtra(SearchActivity.SearchTag, name);
                startActivity(intent);

            }
        });
    }


    @Override
    public void onDestroy() {

        super.onDestroy();
    }


}